using System;
public class POSCD
{
  public static char GetCheckDigit(string code)
  {
    int startFrom=1;
    if(code.Length==0)
      return ' ';
    if(code[0]>='0'&&code[0]<='9')
    {
      startFrom=0;
    }

    int[] weights= { 7,1,3,7,1,3,7,1,3,7,1,3,7,1,3,7,1,3 };
    int sum=0;
    for(int index=startFrom;index<code.Length;index++)
    {
      sum+=((int)code[index]-'0')*weights[index-startFrom];
    }
    sum=10-(sum%10);
    if(sum==10)
    {
      return '0';
    }
    else
    {
      return (char)('0'+sum);
    }
  }

  public static string ApplyCheckDigit(string code)
  {
    if(code.Length==8)
      return string.Format("{0}{1}",code,GetCheckDigit(code));
    throw new Exception("incorrect length - must be 8");
  }

  public static bool Check(string code)
  {
    return (code[code.Length-1]==GetCheckDigit(code.Substring(0,code.Length-1)));
  }

}
